#include "stdafx.h"
#include "stream/base64encode.hpp"

namespace JHCPP
{
	namespace stream
	{
		const unsigned char CBase64EncodeBuf::OUT_ENCODING[64] =
		{
			'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
			'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
			'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
			'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
			'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
			'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
			'w', 'x', 'y', 'z', '0', '1', '2', '3', 
			'4', '5', '6', '7', '8', '9', '+', '/'
		};

		CBase64EncodeBuf::CBase64EncodeBuf(std::ostream& ostr)
		: m_groupLength(0),
		m_pos(0),
		m_lineLength(72),
		m_buf(*ostr.rdbuf())
		{
		}

		CBase64EncodeBuf::~CBase64EncodeBuf()
		{
			try
			{
				close();
			}
			catch (...)
			{
			}
		}

		void CBase64EncodeBuf::setLineLength(int lineLength)
		{
			m_lineLength = lineLength;
		}

		int CBase64EncodeBuf::getLineLength() const
		{
			return m_lineLength;
		}

		int CBase64EncodeBuf::writeToDevice(char c)
		{
			static const int eof = std::char_traits<char>::eof();

			m_group[m_groupLength++] = (unsigned char) c;
			if (m_groupLength == 3)
			{
				unsigned char idx;
				idx = m_group[0] >> 2;
				if (m_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
				idx = ((m_group[0] & 0x03) << 4) | (m_group[1] >> 4);
				if (m_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
				idx = ((m_group[1] & 0x0F) << 2) | (m_group[2] >> 6);
				if (m_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
				idx = m_group[2] & 0x3F;
				if (m_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
				m_pos += 4;
				if (m_lineLength > 0 && m_pos >= m_lineLength) 
				{
					if (m_buf.sputc('\r') == eof) return eof;
					if (m_buf.sputc('\n') == eof) return eof;
					m_pos = 0;
				}
				m_groupLength = 0;
			}
			return charToInt(c);
		}

		int CBase64EncodeBuf::close()
		{
			static const int eof = std::char_traits<char>::eof();

			if (sync() == eof) return eof;
			if (m_groupLength == 1)
			{
				m_group[1] = 0;
				unsigned char idx;
				idx = m_group[0] >> 2;
				if (m_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
				idx = ((m_group[0] & 0x03) << 4) | (m_group[1] >> 4);
				if (m_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
				if (m_buf.sputc('=') == eof) return eof;
				if (m_buf.sputc('=') == eof) return eof;
			}
			else if (m_groupLength == 2)
			{
				m_group[2] = 0;
				unsigned char idx;
				idx = m_group[0] >> 2;
				if (m_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
				idx = ((m_group[0] & 0x03) << 4) | (m_group[1] >> 4);
				if (m_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
				idx = ((m_group[1] & 0x0F) << 2) | (m_group[2] >> 6);
				if (m_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
				if (m_buf.sputc('=') == eof) return eof;
			}
			m_groupLength = 0;
			return m_buf.pubsync();
		}


		CBase64EncodeIOS::CBase64EncodeIOS(std::ostream& ostr) : m_buf(ostr)
		{
		}

		CBase64EncodeIOS::~CBase64EncodeIOS()
		{
		}

		int CBase64EncodeIOS::close()
		{
			return m_buf.close();
		}

		CBase64EncodeBuf* CBase64EncodeIOS::rdbuf()
		{
			return &m_buf;
		}


		CBase64Encode::CBase64Encode(std::ostream& ostr) : CBase64EncodeIOS(ostr), std::ostream(&m_buf)
		{
		}

		CBase64Encode::~CBase64Encode()
		{
		}
	}//end of namespace stream
}//end of namespace JHCPP
